//
// Created by Administrator on 2022/1/26.
//

#ifndef CPP_LEARNING_LEETCODE977_H
#define CPP_LEARNING_LEETCODE977_H
#include <vector>
using namespace std;
#include <math.h>
#include <iostream>
class Solution977 {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> result(nums.size(),0);
        int l = 0; int r = nums.size() -1;
        int k = result.size()-1;
        while (l<r){

            if(abs(nums[l]) > abs(nums[r])){
                result[k] = nums[l] * nums[l];
                --k;
                ++l;
            }else{
                result[k] = nums[r] * nums[r];
                --k;
                --r;
            }
        }

        if(l == r){
            result[k] = nums[l] * nums[l];
            --k;
        }
        return result;
    }
};

void test977(){
    Solution977 s;
    vector<int> nums= {-6,-3,-1,0,2,4,5};
    vector<int> nums2 = s.sortedSquares(nums);

}

#endif //CPP_LEARNING_LEETCODE977_H
